package com.example.entity;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * @author ：songxusheng
 * @date ：Created in 2022/4/11 10:38
 * @description：自定义UserDetails
 * @version: $
 */
@Setter
public class MyUserDetails implements UserDetails {
    private Integer id;

    private String password;

    private final String username;

    private final Collection<? extends GrantedAuthority> authorities;

    /**
     * 账户是否过期
     */
    private final boolean accountNonExpired;

    /**
     * 账户是否被锁定
     */
    private final boolean accountNonLocked;

    /**
     * 密码是否过期
     */
    private final boolean credentialsNonExpired;

    /**
     * 账户是否可用
     */
    private final boolean enabled;

    public MyUserDetails(Integer id, String username, String password, Collection<? extends GrantedAuthority> authorities) {
        this(id, username, password, true, true, true, true, authorities);
    }


    public MyUserDetails(Integer id, String username, String password, boolean enabled, boolean accountNonExpired,
                         boolean credentialsNonExpired, boolean accountNonLocked,
                         Collection<? extends GrantedAuthority> authorities) {
        System.out.println(123);
        Assert.isTrue(username != null && !"".equals(username) && password != null,
                "Cannot pass null or empty values to constructor");
        this.id = id;
        this.username = username;
        this.password = password;
        this.enabled = enabled;
        this.accountNonExpired = accountNonExpired;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.authorities = authorities;
    }

    /**
     * 返回用户权限
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
